.TH std::swap(std::queue) 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::swap(std::queue) \- std::swap(std::queue)

.SH Synopsis
   Defined in header <queue>
   template< class T, class Container >
                                                \fI(since C++11)\fP
   void swap( std::queue<T, Container>& lhs,    \fI(until C++17)\fP

              std::queue<T, Container>& rhs );
   template< class T, class Container >

   void swap( std::queue<T, Container>& lhs,    \fI(since C++17)\fP
              std::queue<T, Container>& rhs )

                  noexcept(/* see below */);

   Specializes the std::swap algorithm for std::queue. Swaps the contents of lhs and
   rhs. Calls lhs.swap(rhs).

   This overload participates in overload resolution only if              \fI(since C++17)\fP
   std::is_swappable_v<Container> is true.

.SH Parameters

   lhs, rhs - containers whose contents to swap

.SH Return value

   \fI(none)\fP

.SH Complexity

   Same as swapping the underlying container.

.SH Exceptions

   noexcept specification:           \fI(since C++17)\fP
   noexcept(noexcept(lhs.swap(rhs)))

.SH Notes

   Although the overloads of std::swap for container adaptors are introduced in C++11,
   container adaptors can already be swapped by std::swap in C++98. Such calls to
   std::swap usually have linear time complexity, but better complexity may be
   provided.

.SH Example


// Run this code

 #include <algorithm>
 #include <iostream>
 #include <queue>

 int main()
 {
     std::queue<int> alice;
     std::queue<int> bob;

     auto print = [](const auto& title, const auto& cont)
     {
         std::cout << title << " size=" << cont.size();
         std::cout << " front=" << cont.front();
         std::cout << " back=" << cont.back() << '\\n';
     };

     for (int i = 1; i < 4; ++i)
         alice.push(i);
     for (int i = 7; i < 11; ++i)
         bob.push(i);

     // Print state before swap
     print("Alice:", alice);
     print("Bob  :", bob);

     std::cout << "-- SWAP\\n";
     std::swap(alice, bob);

     // Print state after swap
     print("Alice:", alice);
     print("Bob  :", bob);
 }

.SH Output:

 Alice: size=3 front=1 back=3
 Bob  : size=4 front=7 back=10
 -- SWAP
 Alice: size=4 front=7 back=10
 Bob  : size=3 front=1 back=3

.SH See also

   swap    swaps the contents
   \fI(C++11)\fP \fI(public member function)\fP

.SH Category:
     * conditionally noexcept
